package app.agent.presentation

import im.mange.jetboot._
import im.mange.jetboot.bootstrap3.Bootstrap
import jetboot._
import Html._
import Bootstrap._
import Css._
import app.server.services.ServiceFactory._
import im.mange.shoreditch.hipster.Action
import scala.Some
import im.mange.shoreditch.engine.{Clock, DateFormatForHumans}

//TODO: is reasons actually applicable here?
case class ActionRenderer(a: Action, reasons: List[String])(implicit clock: Clock) extends Renderable {
  def render = Composite(
    div(
//      span(span().classes(classMeUp).styles(marginRight("5px")), R(a.pure)
      span(span(a.id.toString).classes("badge").styles(fontSize(xSmall)), R(" " + a.pure)
      ).classes(pullLeft),
      SystemRenderer(a.inApp)
    ).styles(clear(both)),
    div(
      span(a.mangledIn.getOrElse(a.in).map(i => span(span(i.name + " ").styles(fontSize(smaller), color("darkgray")), span(i.value.getOrElse("-")).styles(fontSize(smaller))).styles(marginLeft("5px"))):_*).classes(pullLeft),//.styles(fontSize(smaller))
      span(a.replacedReturnValue + " ").styles(fontSize(smaller), marginTop("2px"), color("darkgray")).classes(pullRight)//.styles(fontSize(smaller))
    ).styles(clear(both)),
    div(
      span(StatusRenderer(status, a.script.isCurrentlyRunningStep(a)), R(when + message)).classes(pullLeft).styles(fontSize(xSmall))
    ).styles(clear(both))
  ).render

  private def when = if (a.script.hasStarted && (a.script.isCurrentlyRunningStep(a) || a.isCompleted)) DateFormatForHumans.formattedNow(clock) + " " else ""

  private def message = {
    val m = if (reasons.isEmpty && a.script.isCurrentlyRunningStep(a)) "Running..."
    else reasons.mkString(",")
    if (!m.isEmpty) "- " + m else m
  }

  private def status =
    if (!reasons.isEmpty) Some(false)
    else if (a.isCompleted) Some(true)
    else None
}
